package com.shouhuobao.take.parcel;

import android.app.Activity;
import android.content.Intent;
import android.os.Message;
import android.text.Editable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.tools.TextWatcherExt;
import com.shouhuobao.station.R;
import com.shouhuobao.take.model.ParcelBean;
import com.shouhuobao.take.model.PostCompanyBean;

import java.util.ArrayList;

/**
 * Created by joker on 2017/5/25. <br/>
 * 已取走的包裹列表
 */
public class Parcel2Fragment extends BaseParcelFragment implements View.OnClickListener {
    private EditText mChoosePostCompanyTV;// 选择快递公司
    private EditText mChooseDateTV;// 选择日期
    private TextView mParcelCount;// 当前包裹数量
    private int mDateType = 0;// datetype 0按月选择的日期 1按日选择的日期

    @Override
    protected void findViewById() {
        setContentView(R.layout.take_parcel_list);
        super.findViewById();
        View headerView = LayoutInflater.from(getActivityContext()).inflate(R.layout.take_parcel_takse_header, null);
        mChoosePostCompanyTV = (EditText) headerView.findViewById(R.id.choosepostET);
        mChooseDateTV = (EditText) headerView.findViewById(R.id.choosedateET);
        mParcelCount = (TextView) headerView.findViewById(R.id.parcel_count);
        mAdapter = new ParcelAdapter(new ArrayList<ParcelBean>(), getActivityContext());
        mListView.setAdapter(mAdapter);
        // 先不做搜索功能
        mListView.addHeaderView(headerView);
        mChoosePostCompanyTV.setOnClickListener(this);
        mChooseDateTV.setOnClickListener(this);
        mChoosePostCompanyTV.addTextChangedListener(new TextWatcherExt() {
            @Override
            public void afterTextChanged(Editable s) {
                expressCompany = s.toString();
            }
        });
        mChooseDateTV.addTextChangedListener(new TextWatcherExt() {
            @Override
            public void afterTextChanged(Editable s) {
                date = s.toString();
            }
        });
        mChooseDateTV.setGravity(Gravity.CENTER);
        mParcelCount.setVisibility(View.GONE);
        //
        showLoadingDialog(null);
        super.parcelType = 2;
        AppHttp.getInstance().takeParcelQueryList(parcelType, 1, null, null);
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            // 选择快递公司
            case R.id.choosepostET:
                intent = new Intent(getActivityContext(), ParcelExpressCompanyActivity.class);
                startActivityForResult(intent, 100);
                break;
            // 选择日期
            case R.id.choosedateET:
                intent = new Intent(getActivityContext(), ParcelSelectDateActivity.class);
                startActivityForResult(intent, 101);
                break;
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.take_parcel_takes:
                final ReqResult<ParcelBean> result = super.handleMessage(msg.obj);
                getActivityContext().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mParcelCount.setText("当前共" + result.getTotal() + "个包裹");
                        mParcelCount.setVisibility(View.VISIBLE);
                    }
                });
                return true;
            default:
                return super.handleMessage(msg);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK && requestCode == 100) {
            PostCompanyBean postCompanyBean = (PostCompanyBean) data.getSerializableExtra("item");
            if (postCompanyBean != null) {
                mChoosePostCompanyTV.setText(postCompanyBean.getName());
                queryParcel();
            }
        } else if (resultCode == Activity.RESULT_OK && requestCode == 101) {
            // type 0按月选择的日期 1按日选择的日期
            mDateType = data.getIntExtra("type", 0);
            if (mDateType == 0) {
                String date = data.getStringExtra("date");
                mChooseDateTV.setText(date);
            } else {
                int datetype = data.getIntExtra("datetype", 0);
                String startdate = data.getStringExtra("startdate");
                String enddate = data.getStringExtra("enddate");
                if (datetype == 1) {
                    mChooseDateTV.setText(startdate);
                } else {
                    mChooseDateTV.setText(startdate + "-" + enddate);
                }
            }
            queryParcel();
        }
    }

    private void queryParcel() {
        super.expressCompany = mChoosePostCompanyTV.getText().toString();
        super.date = mChooseDateTV.getText().toString();
        if (!isEmpty(expressCompany) || !isEmpty(date)) {
            // 搜索
            AppHttp.getInstance().takeParcelQueryList(2, 1, expressCompany, date);
        } else {
            return;
        }
    }

}
